/*
  ==============================================================================

    Generator.cpp
    Created: 30 Nov 2014 5:16:40pm
    Author:  Alistair Barker

  ==============================================================================
*/

#include "Generator.h"

Generator::Generator()
{
    for (int i = 0; i < wave_length; i++) {
        sinWavetable[i] = sin(i*2*float_Pi/wave_length);
    }
    
    //needs to be unaliased
    
    for (int i = 0; i < (wave_length/2); i++) {
        squareWavetable[i] = 1.0;
    }
    
    for (int i = (wave_length/2); i < wave_length; i++)
    {
        squareWavetable[i] = -1.0;
    }
    
    
    //Saw and trianlge don't work yet!!!!
    for (int i = 0; i < wave_length; i++) {
        sawWavetable[i] = -1.0 + 2*i/wave_length;
    }
    
    for (int i = 0; i < (wave_length/2); i++) {
        triangleWavetable[i] = -1.0 + 4*i/wave_length;
    }
    
    for (int i = (wave_length/2); i < wave_length; i++)
    {
        triangleWavetable[i] = 1.0 - 4*(i-wave_length/2)/wave_length;
    }
    
}

double Generator::getNextSample()
{
    switch (type) {
        case kSine:
            return sinWavetable[readHead];
            break;
            
        case kSquare:
            return squareWavetable[readHead];
            break;
            
        case kWhiteNoise:
            return double(rand())/double(RAND_MAX)*2 - 1.0;
            break;
        
        default:
            return 0.0;
        
    }
    
    readHead++;
    
    
}
